package com.sq.partner.manager.util.excel;

import org.apache.poi.ss.usermodel.*;

public class POIStyleWrapper {
	private CellStyle cellStyle = null;
	private Font font = null;
    private CreationHelper createHelper = null;
    public CellStyle getCellStyle() {
        return cellStyle;
    }

    public POIStyleWrapper(Sheet sheet){
        this.cellStyle = sheet.getWorkbook().createCellStyle();
        this.font = sheet.getWorkbook().createFont();
        this.createHelper = sheet.getWorkbook().getCreationHelper();
    }
    public POIStyleWrapper(Workbook workbook){
        this.cellStyle = workbook.createCellStyle();
        this.font = workbook.createFont();
        this.createHelper = workbook.getCreationHelper();
    }
    //设置水平对齐
    //CellStyle.ALIGN_CENTER
    public POIStyleWrapper setAlignment(short halign) {
        cellStyle.setAlignment(halign);
        return this;
    }
    //设置垂直对齐
    //CellStyle.VERTICAL_BOTTOM
    public POIStyleWrapper setVerticalAlignment(short valign) {
        cellStyle.setVerticalAlignment(valign);
        return this;
    }
    //设置边框  start
    //黑色实线边框
    public POIStyleWrapper setSolidBorder() {
        cellStyle.setBorderBottom(CellStyle. BORDER_THIN);
        cellStyle.setBottomBorderColor(IndexedColors. BLACK.getIndex());
        cellStyle.setBorderLeft(CellStyle. BORDER_THIN);
        cellStyle.setLeftBorderColor(IndexedColors. BLACK.getIndex());
        cellStyle.setBorderRight(CellStyle. BORDER_THIN);
        cellStyle.setRightBorderColor(IndexedColors. BLACK.getIndex());
        cellStyle.setBorderTop(CellStyle. BORDER_THIN);
        cellStyle.setTopBorderColor(IndexedColors. BLACK.getIndex());
        return this;
    }
    //IndexedColors.BLACK.getIndex()
    public POIStyleWrapper setSolidBorder(short borderColor) {
        cellStyle.setBorderBottom(CellStyle. BORDER_THIN);
        cellStyle.setBottomBorderColor(borderColor);
        cellStyle.setBorderLeft(CellStyle. BORDER_THIN);
        cellStyle.setLeftBorderColor(borderColor);
        cellStyle.setBorderRight(CellStyle. BORDER_THIN);
        cellStyle.setRightBorderColor(borderColor);
        cellStyle.setBorderTop(CellStyle. BORDER_THIN);
        cellStyle.setTopBorderColor(borderColor);
        return this;
    }
    //CellStyle.BORDER_THIN,IndexedColors.BLACK.getIndex()
    public POIStyleWrapper setBorder(short borderType,short borderColor) {
        cellStyle.setBorderBottom(borderType);
        cellStyle.setBottomBorderColor(borderColor);
        cellStyle.setBorderLeft(borderType);
        cellStyle.setLeftBorderColor(borderColor);
        cellStyle.setBorderRight(borderType);
        cellStyle.setRightBorderColor(borderColor);
        cellStyle.setBorderTop(borderType);
        cellStyle.setTopBorderColor(borderColor);
        return this;
    }
    //设置边框  end

    //设置背景色
    //CellStyle.BORDER_THIN,IndexedColors.BLACK.getIndex()
    public POIStyleWrapper setBackGround(short backgroundColor) {
        //POI 中的前景色就是Excel 中的背景色
        cellStyle.setFillForegroundColor(backgroundColor);
        //cellStyle.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
        cellStyle.setFillPattern(CellStyle. SOLID_FOREGROUND);   //填充样式，写死，很少使用其他样式的
        return this;
    }


    //设置字体  start
    //字体: Arial、宋体、Courier New
    //设置字体: 字体名，字体高度，不加粗，字体颜色
    public POIStyleWrapper setBoldFont(String fontName,int fontHeight,short color) {
        this.setFont(fontName,fontHeight,Font. BOLDWEIGHT_BOLD,color);
        return this;
    }
    //设置字体: 字体名，字体高度，加粗，字体颜色
    public POIStyleWrapper setNormalFont(String fontName,int fontHeight,short color) {
        this.setFont(fontName, fontHeight, Font.BOLDWEIGHT_NORMAL ,color);
        return this;
    }
    //设置字体: 字体名，字体高度，不加粗，字体颜色(默认黑)
    public POIStyleWrapper setBoldFont(String fontName,int fontHeight) {
        this.setFont(fontName,fontHeight,Font. BOLDWEIGHT_BOLD);
        return this;
    }
    //设置字体: 字体名，字体高度，加粗，字体颜色(默认黑)
    public POIStyleWrapper setNormalFont(String fontName,int fontHeight) {
        this.setFont(fontName, fontHeight, Font.BOLDWEIGHT_NORMAL );
        return this;
    }
    //设置字体: 字体名，字体高度，是否加粗，字体颜色(默认黑)
    public POIStyleWrapper setFont(String fontName,int fontHeight,int boldWeight) {
        this.setFont(fontName, fontHeight, boldWeight, IndexedColors.BLACK .getIndex());
        return this;
    }
    //设置字体: 字体名，字体高度，是否加粗，字体颜色
    //如果只有POI core ，字体高度不好使
    public POIStyleWrapper setFont(String fontName,int fontHeight,int boldWeight,short color) {
        font.setFontName(fontName);
        font.setFontHeightInPoints(( short)fontHeight); //几号字
        font.setBoldweight((short)boldWeight);
        font.setColor(color);
        //font.setItalic(true);
        //font.setStrikeout(true);
        cellStyle.setFont(font);
        return this;
    }
    //设置字体  end

    //设置单元格内容自动换行
    //Row 和Column 还要做特殊的设置 TODO
    public POIStyleWrapper setAutoWrap() {
        cellStyle.setWrapText( true);
        return this;
    }

    //设置单元格数据类型(内建数据类型): 属于样式的一部分
    public POIStyleWrapper setDataFormat(String formatString) {
        cellStyle.setDataFormat(
                createHelper.createDataFormat().getFormat(formatString));
        return this;
    }

}
